package com.cloud.train.member.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.jwt.JWTUtil;
import com.cloud.train.common.exception.BusinessException;
import com.cloud.train.common.exception.BusinessExceptionEnum;
import com.cloud.train.common.util.JwtUtil;
import com.cloud.train.common.util.SnowUtil;
import com.cloud.train.member.domain.Member;
import com.cloud.train.member.domain.MemberExample;
import com.cloud.train.member.mapper.MemberMapper;
import com.cloud.train.member.req.MemberLoginReq;
import com.cloud.train.member.req.MemberRegisterReq;
import com.cloud.train.member.req.MemberSetCode;
import com.cloud.train.member.resp.MemberLoginResp;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class MemberService {
    @Resource
    private MemberMapper memberMapper;
    public int count(){
        return Math.toIntExact(memberMapper.countByExample(null));
    }
    public long register(MemberRegisterReq req){
        String mobile = req.getMobile();
        Member list = slectMember(mobile);
        if (ObjectUtil.isNull(list)){
//            return list.get(0).getId();
//            throw new RuntimeException("手机号已被注册");
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_EXIST);
        }
        Member member = new Member();
        member.setId(SnowUtil.getSnowflakeNextId());
        member.setMobile(mobile);
        memberMapper.insert(member);
        System.out.println(memberMapper);
        return member.getId();
    }

    public void setCode(MemberSetCode req){
        String mobile = req.getMobile();
        Member members = slectMember(mobile);
        if (ObjectUtil.isNull(members)){
            log.info("手机号不存在，插入一条数据");
            Member member = new Member();
            member.setId(SnowUtil.getSnowflakeNextId());
            member.setMobile(mobile);
            memberMapper.insert(member);
        }
        else {
            log.info("手机号存在，不插入记录");
        }
//        String s = RandomUtil.randomString(4);
        //测试使用，后续改变
        String s = "8888";
        log.info("生成短信验证码:{}",s);
    }
    public MemberLoginResp Login(MemberLoginReq req){
        String mobile = req.getMobile();
        String code = req.getCode();
        Member members = slectMember(mobile);
        if (ObjectUtil.isNull(members)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_NOT_EXIST);
        }
            if (!"8888".equals(code)){
                throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_CODE_ERROR);
        }

        MemberLoginResp loginResp = BeanUtil.copyProperties(members, MemberLoginResp.class);
        String token = JwtUtil.createToken(loginResp.getId(),loginResp.getMobile());
        loginResp.setToken(token);
            return loginResp;

    }

    private Member slectMember(String mobile) {
        MemberExample memberExample = new MemberExample();
        memberExample.createCriteria().andMobileEqualTo(mobile);
        List<Member> members = memberMapper.selectByExample(memberExample);
       if (CollUtil.isEmpty(members)){
           return null;
       }
       else {
           return members.get(0);
       }
    }
}
